package com.itheima.bookdao;

import com.itheima.domain.Book;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class BookFileDao implements BaseBookDao {

    private static final String BOOK_INFO_PATH = "itheima-book-manager\\bookInfo.txt";

    private static final String DATE_PATTERN = "yyyy-MM-dd";

    @Override
    public Book[] getAllBooks() {

        //创建一个ArrayList集合对象
        ArrayList<Book> list = new ArrayList<Book>();

        BufferedReader bufferedReader = null;
        try {

            File file = new File(BOOK_INFO_PATH);
            if(!file.exists()) {
                file.createNewFile();
            }

            //创建BufferedReader对象
            bufferedReader = new BufferedReader(new FileReader(BOOK_INFO_PATH));

            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                String[] split = line.split(",");
                Book bookInArr = new Book();
                bookInArr.setBookId(split[0]);
                bookInArr.setBookName(split[1]);

                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_PATTERN);
                Date publishDate = simpleDateFormat.parse(split[2]);
                bookInArr.setPublishDate(publishDate);

                double price = Double.parseDouble(split[3]);
                bookInArr.setPrice(price);

                list.add(bookInArr);

            }

            Book[] books = new Book[list.size()];
            for(int x = 0; x < list.size(); x++) {
                Book bookInArr = list.get(x);
                books[x] = bookInArr;
            }

            return books;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return new Book[0];
    }

    @Override
    public boolean addBook(Book book) {

        BufferedWriter bufferedWriter = null;
        try {

            bufferedWriter = new BufferedWriter(new FileWriter(BOOK_INFO_PATH, true));

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_PATTERN);
            String format = simpleDateFormat.format(book.getPublishDate());
            String line = book.getBookId() + "," + book.getBookName() + "," + format + "," + book.getPrice();

            bufferedWriter.write(line);
            bufferedWriter.newLine();
            bufferedWriter.flush();

            return true;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }



        return false;
    }

    @Override
    public void deleteBook(String bookId) {

        Book[] allBooks = getAllBooks();

        int index = -1;
        for(int x = 0; x < allBooks.length; x++) {
            Book bookInArr = allBooks[x];
            if(bookInArr != null) {
                if(bookInArr.getBookId().equals(bookId)) {
                    index = x;
                    break;
                }
            }
        }

        if(index != -1) {
            allBooks[index] = null;
        }

        BufferedWriter bufferedWriter = null;
        try {

            bufferedWriter = new BufferedWriter(new FileWriter(BOOK_INFO_PATH));

            for(int x = 0; x < allBooks.length; x++) {

                Book bookInArr = allBooks[x];
                if(bookInArr != null) {

                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_PATTERN);
                    String format = simpleDateFormat.format(bookInArr.getPublishDate());
                    String line = bookInArr.getBookId() + "," + bookInArr.getBookName() + "," + format + "," + bookInArr.getPrice();

                    bufferedWriter.write(line);
                    bufferedWriter.newLine();
                    bufferedWriter.flush();

                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Override
    public void updateBook(Book book) {

        Book[] allBooks = getAllBooks();

        int index = -1;
        for(int x = 0; x < allBooks.length; x++) {
            Book bookInArr = allBooks[x];
            if(bookInArr != null) {
                if(bookInArr.getBookId().equals(book.getBookId())) {
                    index = x;
                    break;
                }
            }
        }

        if(index != -1) {
            allBooks[index] = book;
        }

        BufferedWriter bufferedWriter = null;
        try {

            bufferedWriter = new BufferedWriter(new FileWriter(BOOK_INFO_PATH));

            for(int x = 0; x < allBooks.length; x++) {

                Book bookInArr = allBooks[x];

                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_PATTERN);
                String format = simpleDateFormat.format(bookInArr.getPublishDate());
                String line = bookInArr.getBookId() + "," + bookInArr.getBookName() + "," + format + "," + bookInArr.getPrice();

                bufferedWriter.write(line);
                bufferedWriter.newLine();
                bufferedWriter.flush();

            }

        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
